package com.ztesoft.ossup.report.web.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztesoft.ossup.report.domain.Report;
import com.ztesoft.ossup.report.service.DataPreviewService;
import com.ztesoft.ossup.report.service.SqlConfigService;
import com.ztesoft.ossup.report.web.BaseController;

@Controller
@RequestMapping("/report/sqlConfig")
public class SqlConfigController extends BaseController{
	
	@Autowired
	SqlConfigService SqlConfigService;
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String sqlConfig(HttpServletRequest request) {
		return "/config/sqlConfig";
	}
	
	@Autowired
	DataPreviewService dataPreviewService;
	
	@RequestMapping(value = "/getTableAndView", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,List<Map<String, Object>>> getTableAndView(){
		List<Map<String, Object>> tablesList = null;
		List<Map<String, Object>> viewsList = null;
		Map<String,List<Map<String, Object>>> retMap = new HashMap<>();
		try {
			tablesList = SqlConfigService.getTablesDefaultUser();
			viewsList = SqlConfigService.getViewsDefaultUser();
			retMap.put("tablesList", tablesList);
			retMap.put("viewsList", viewsList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retMap;
	}
	
	@RequestMapping(value = "/getColsByTableName", method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> getColsByTableName(@RequestBody String tableName){
		List<Map<String, Object>> colsList = null;
		tableName = tableName.replaceAll("\"", "");
		try {
			colsList = SqlConfigService.getColsByTableName(tableName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return colsList;
	}
	
	@RequestMapping(value = "/listCols", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,List<Map<String, Object>>> getColsByTableName(@RequestBody Map tableMap){
		List<Map<String, Object>> leftTableColsList = null;
		List<Map<String, Object>> rightTableColsList = null;
		Map<String, List<Map<String, Object>>> retMap = new HashMap<String, List<Map<String, Object>>>();
		String leftTable = tableMap.get("leftTable").toString();
		String rightTable = tableMap.get("rightTable").toString();
		try {
			leftTableColsList = SqlConfigService.getColsByTableName(leftTable);
			rightTableColsList = SqlConfigService.getColsByTableName(rightTable);
			retMap.put("leftTableColsList", leftTableColsList);
			retMap.put("rightTableColsList", rightTableColsList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retMap;
	}
	
	@RequestMapping(value = "/data", method = RequestMethod.POST)
	@ResponseBody
    public com.ztesoft.ossup.report.domain.PageData getData(@RequestBody Map<String, Object> paramMap) {
		int offset = (int) paramMap.get("offset");
		int limit = (int) paramMap.get("limit");
		String assembleSql = (String) paramMap.get("assembleSql");
		
		//只需要截取20条来作为显示数据
		String dataSql = dataPreviewService.doSqlNoParamsForSqlConfig(assembleSql);
		
		com.ztesoft.ossup.report.domain.PageData pageData = dataPreviewService.getDataBySql(dataSql, offset, limit,10);
		
        return pageData;
    }
	
	
}
